package cn.com.allunion.sms.service.mybatis.impl;

import cn.com.allunion.sms.mapper.UserbaseSmsbusinessMapper;
import cn.com.allunion.sms.pojo.UserbaseSmsbusiness;
import cn.com.allunion.sms.pojo.UserbaseSmsbusinessExample;
import cn.com.allunion.sms.service.mybatis.UserbaseSmsbusinessService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户所拥有业务关系 业务逻辑接口 实现类
 * Created by YangJie on 2016/4/18.
 */
@Service
@Transactional
public class UserbaseSmsbusinessServiceImpl implements UserbaseSmsbusinessService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserbaseSmsbusinessServiceImpl.class) ;

    @Autowired
    UserbaseSmsbusinessMapper userbaseSmsbusinessMapper ;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<UserbaseSmsbusiness> findByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            LOGGER.warn("the userId object is empty!");
            return null ;
        }
        UserbaseSmsbusinessExample example = new UserbaseSmsbusinessExample();
        UserbaseSmsbusinessExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId) ;

        return userbaseSmsbusinessMapper.selectByExample(example) ;
    }


    @Override
    public boolean deleteByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            LOGGER.warn("the userId object is empty!");
            return false ;
        }

        UserbaseSmsbusinessExample example = new UserbaseSmsbusinessExample();
        UserbaseSmsbusinessExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        int result = userbaseSmsbusinessMapper.deleteByExample(example) ;

        return (result > 0) ;
    }

    @Override
    public boolean deleteById(String id) {
        if (StringUtils.isBlank(id)) {
            LOGGER.warn("the id object is empty!");
            return false ;
        }

        int result = userbaseSmsbusinessMapper.deleteByPrimaryKey(id) ;

        return (result > 0) ;
    }

    @Override
    public boolean save(UserbaseSmsbusiness userbaseSmsbusiness) {
        if (null == userbaseSmsbusiness) {
            LOGGER.warn("the userbaseSmsbusiness object is null.");
            return false ;
        }

        int result = userbaseSmsbusinessMapper.insert(userbaseSmsbusiness);

        return (result > 0) ;
    }

    @Override
    public boolean batchSave(List<UserbaseSmsbusiness> usList) {
        if (CollectionUtils.isEmpty(usList)) {
            LOGGER.warn("the userbaseSmsbusiness objects list is empty.");
            return false ;
        }

        boolean result = false ;
        for (UserbaseSmsbusiness us : usList) {
            result = save(us) ;
        }

        return result;
    }

    @Override
    public boolean setBusinesses(List<UserbaseSmsbusiness> usList) {
        if (CollectionUtils.isEmpty(usList)) {
            LOGGER.warn("the userbaseSmsbusiness objects list is empty.");
            return false ;
        }

        // 解除原来的关联关系
        deleteByUserId(usList.get(0).getUserId()) ;

        // 保存新的关系
        batchSave(usList) ;

        return true;
    }
}
